/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelLlantaReencauche.java
 *
 * Created on 15-jun-2012, 18:01:58
 */
package com.camachosanchez.gui;

import com.camachosanchez.bean.DetalleControlReencauche;
import com.camachosanchez.bean.Neumaticos;
import com.camachosanchez.bean.TipoPiso;
import com.camachosanchez.bean.TiposBandaNeumatico;
import com.camachosanchez.controler.jpa.NeumaticosJpaController;
import com.camachosanchez.controler.jpa.TipoPisoJpaController;
import com.camachosanchez.controler.jpa.TiposBandaNeumaticoJpaController;
import com.camachosanchez.gui.textfield.MaxLengthTextDocument;
import com.camachosanchez.util.Constantes;
import java.awt.Dimension;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Rolando
 */
public class PanelLlantaReencauche extends javax.swing.JPanel {

    private DetalleControlReencauche detalle;
    private int id_llanta;
    private EnvioReencauche contenedor;

    /** Creates new form PanelLlantaReencauche */
    public PanelLlantaReencauche(EnvioReencauche contenedor, int id) {
        initComponents();
        int ancho = 619;
        int alto = 22;
        setMaximumSize(new Dimension(ancho, alto));
        setMinimumSize(new Dimension(ancho, alto));
        setPreferredSize(new Dimension(ancho, alto));
        setSize(new Dimension(ancho, alto));
        id_llanta = id;
        detalle = new DetalleControlReencauche();
        TiposBandaNeumaticoJpaController controller = new TiposBandaNeumaticoJpaController(null);
        TipoPisoJpaController controllerPiso = new TipoPisoJpaController(null);
        List<TiposBandaNeumatico> bandas = controller.findTiposBandaNeumaticoEntities();
        List<TipoPiso> tipoPisos = controllerPiso.findTipoPisoEntities();
        comboTiposBanda.setModel(new DefaultComboBoxModel(bandas.toArray()));
        cmbTipoPiso.setModel(new DefaultComboBoxModel(tipoPisos.toArray()));
        MaxLengthTextDocument doc_precio = new MaxLengthTextDocument();
        doc_precio.setMaxChars(8, Constantes.DECIMALES);
        txtPrecio.setDocument(doc_precio);
        txtPrecio.setText("0.00");
        this.contenedor = contenedor;
        NeumaticosJpaController neu = new NeumaticosJpaController(null);
        Neumaticos temp = neu.findNeumaticos(id);
        txtCodigo.setText(temp.getCodigo());
        txtBandaAntigua.setText((temp.getIdBanda() != null)? temp.getIdBanda().getNombre() : "");
        txtPisoAntiguo.setText((temp.getTipoPiso() != null)? temp.getTipoPiso().getNombre() : "");
    }

    public int getId_llanta() {
        return id_llanta;
    }

//    @Override
//    public void paintComponent(Graphics g) {
//        Graphics2D graphics = (Graphics2D) g;
//        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        int round = 20;
//        int w = this.getWidth();
//        int h = this.getHeight();
//        graphics.setColor(Color.white);
//        graphics.fillRect(0, 0, w, h);
//        Paint oldPaint = graphics.getPaint();
//        GradientPaint paint = new GradientPaint(w / 2, 0, new Color(250, 250, 250), w / 2, h, new Color(190, 190, 190), true);
//        graphics.setPaint(paint);
//        graphics.fillRoundRect(4, 4, w - 8, h - 8, round, round);
//        graphics.setColor(new Color(190, 190, 190));
//        graphics.drawRoundRect(4, 4, w - 8, h - 8, round, round);
//        graphics.setPaint(oldPaint);
//    }
    public DetalleControlReencauche guardar() {
        if(comboTiposBanda.getSelectedItem() == null){
            JOptionPane.showMessageDialog(this, "Seleccione una nueva banda para el neumático. Si no existe una, ingrésela desde el menú Logística.", "Datos incorrectos", JOptionPane.INFORMATION_MESSAGE);
            comboTiposBanda.requestFocusInWindow();
            return null;
        }
        detalle.setIdNuevoTipoBanda((TiposBandaNeumatico) comboTiposBanda.getSelectedItem());
        detalle.setIdNuevoTipoPiso((TipoPiso) cmbTipoPiso.getSelectedItem());
        try {
            Double precio = Double.parseDouble(txtPrecio.getText());
            detalle.setPrecio(precio);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "El valor ingresado para el precio debe ser un número decimal.", "Datos incorrectos", JOptionPane.INFORMATION_MESSAGE);
            txtPrecio.requestFocusInWindow();
            return null;
        }
        detalle.setEstado(true); //Visible
        detalle.setFechaInsercionActualizacion(new Date());
        detalle.setIdNeumatico(new Neumaticos(id_llanta));
        detalle.setPorGarantia(checkGarantia.isSelected());
//        NeumaticoDetalleControlReencauche asociacion = new NeumaticoDetalleControlReencauche();
//        asociacion.setIdNeumatico(new Neumaticos(id_llanta));
//        asociacion.setIdDetalleControlReencauche(detalle);
//        List<NeumaticoDetalleControlReencauche> asociaciones = new ArrayList<NeumaticoDetalleControlReencauche>();
//        asociaciones.add(asociacion);
//        detalle.setNeumaticoDetalleControlReencaucheList(asociaciones);
        return detalle;

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        txtCodigo = new javax.swing.JTextField();
        txtBandaAntigua = new javax.swing.JTextField();
        txtPisoAntiguo = new javax.swing.JTextField();
        comboTiposBanda = new javax.swing.JComboBox();
        cmbTipoPiso = new javax.swing.JComboBox();
        txtPrecio = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        checkGarantia = new javax.swing.JCheckBox();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout(1, 0));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/camachosanchez/recursos/16_dar_de_baja.png"))); // NOI18N
        jButton1.setBorderPainted(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setMaximumSize(new java.awt.Dimension(20, 20));
        jButton1.setMinimumSize(new java.awt.Dimension(20, 20));
        jButton1.setPreferredSize(new java.awt.Dimension(20, 20));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1, java.awt.BorderLayout.LINE_START);

        jPanel8.setBackground(new java.awt.Color(255, 255, 255));
        jPanel8.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 2, 2, 2));
        jPanel8.setLayout(new java.awt.GridLayout(1, 4, 4, 0));

        txtCodigo.setEditable(false);
        txtCodigo.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jPanel8.add(txtCodigo);

        txtBandaAntigua.setEditable(false);
        txtBandaAntigua.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jPanel8.add(txtBandaAntigua);

        txtPisoAntiguo.setEditable(false);
        jPanel8.add(txtPisoAntiguo);

        comboTiposBanda.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel8.add(comboTiposBanda);

        cmbTipoPiso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel8.add(cmbTipoPiso);

        txtPrecio.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtPrecio.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtPrecioFocusGained(evt);
            }
        });
        jPanel8.add(txtPrecio);

        checkGarantia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkGarantiaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(checkGarantia, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(30, 30, 30))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(checkGarantia)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jPanel8.add(jPanel1);

        jPanel2.add(jPanel8, java.awt.BorderLayout.CENTER);

        add(jPanel2, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    contenedor.eliminar(this);
    contenedor.borrarLlanta(this.id_llanta);
//    contenedor.llenarTabla("");
    
}//GEN-LAST:event_jButton1ActionPerformed

    private void checkGarantiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkGarantiaActionPerformed
        // TODO add your handling code here:
        txtPrecio.setEnabled(!checkGarantia.isSelected());
        if(!txtPrecio.isEnabled()){
            txtPrecio.setText("0.00");
        }
    }//GEN-LAST:event_checkGarantiaActionPerformed

    private void txtPrecioFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPrecioFocusGained
        // TODO add your handling code here:
        txtPrecio.selectAll();
    }//GEN-LAST:event_txtPrecioFocusGained

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox checkGarantia;
    private javax.swing.JComboBox cmbTipoPiso;
    private javax.swing.JComboBox comboTiposBanda;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JTextField txtBandaAntigua;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtPisoAntiguo;
    private javax.swing.JTextField txtPrecio;
    // End of variables declaration//GEN-END:variables
}
